package hubbub

import static org.junit.Assert.*
import org.junit.*

class PostIntegrationTestTests {

	@Before
	void setUp() {
		// Setup logic here
	}

	@After
	void tearDown() {
		// Tear down logic here
	}

	@Test
	void testFirstPost() {
		def user = new User(userId: 'Joe', password: 'secret').save()

		def post1 = new Post(content: "First post...W00t!")
		user.addToPosts(post1)


		def post2 = new Post(content: "Second post...")
		user.addToPosts(post2)

		def post3 = new Post(content: "Third post...")
		user.addToPosts(post3)

		assertEquals 3, User.get(user.id).posts.size()
	}

	@Test
	void testAccessingPosts() {
		def user = new User(userId: 'Joe', password: 'secret').save()
		user.addToPosts(new Post(content: "First"))
		user.addToPosts(new Post(content: "Second"))
		user.addToPosts(new Post(content: "Third"))

		def foundUser = User.get(user.id)
		def postNames = foundUser.posts.collect {it.content}
		assertEquals(['First', 'Second', 'Third'], postNames.sort())
	}

	@Test
	void testPostWithTags() {
		def user = new User(userId: 'Joe', password: 'secret').save()

		def tagGroovy = new Tag(name: 'groovy')
		def tagGrails = new Tag(name: 'grails')
		user.addToTags(tagGroovy)
		user.addToTags(tagGrails)

		def tagNames = user.tags*.name
		assertEquals(['grails', 'groovy'], tagNames.sort())

		def groovyPost = new Post(content: "A groovy post")
		user.addToPosts(groovyPost)
		groovyPost.addToTags(tagGroovy)
		assertEquals 1, groovyPost.tags.size()

		def bothPost = new Post(content: "A groovy and grails post")
		user.addToPosts(bothPost)
		bothPost.addToTags(tagGroovy)
		bothPost.addToTags(tagGrails)
		assertEquals 2, bothPost.tags.size()
	}

	@Test
	void testFollowing() {
		def glen = new User(userId: 'Glen', password: 'secret').save()
		def peter = new User(userId: 'Peter', password: 'secret').save()
		def sven = new User(userId: 'Sven', password: 'secret').save()

		glen.addToFollowing(peter)
		glen.addToFollowing(sven)
		assertEquals 2, glen.following.size()

		sven.addToFollowing(peter)
		assertEquals 1, sven.following.size()
	}
}
